<?php

namespace beObject\Web;

use beObject\Object;
use beObject\Primitive\String;

/**
 * This class implement methods to interact with an uploaded file.<br />
 * Store the filename in private variable.<br />
 *
 * @author Dany Mottier
 * @version 2.3
 */
class UploadedFile
    extends Object {

  /** @var string The name of the uploaded file. */
  private $filename = "";

  /**
   * Store the filename in private variable.<br />
   *
   * @param string $fieldname The filename.
   */
  public function __construct(String $filename) {
    $this->filename = $filename();
  }

  /**
   * Returns the simple name of the file.<br />
   *
   * @return string The filename.
   */
  public function getBaseName() {
    return basename($_FILES[$this->filename]['name']);
  }

  /**
   * Returns the name of the file.<br />
   *
   * @return string The name of the file.
   */
  public function getName() {
    return $_FILES[$this->filename]['name'];
  }

  /**
   * Returns the type of the file.<br />
   *
   * @return string The type of the file.
   */
  public function getFileType() {
    return $_FILES[$this->filename]['type'];
  }

  /**
   * Returns the file extension.<br />
   *
   * @return string The file extension.
   */
  public function getExtension() {
    $filename = $this->getBaseName();

    return substr($filename, strrpos($filename, ".") + 1);
  }

  /**
   * Returns the temporary associated name of the file.<br />
   *
   * @return string The temporary associated name of the file.
   */
  public function getTemporaryName() {
    return $this->getTmpName();
  }

  /**
   * Alias of getTemporaryName()
   *
   * @see getTemporaryName()
   */
  public function getTmpName() {
    return $_FILES[$this->filename]['tmp_name'];
  }

  /**
   * Returns the error if exists.<br />
   *
   * @return string The error.
   */
  public function getError() {
    return $_FILES[$this->filename]['error'];
  }

  /**
   * Return a significant error message.<br />
   *
   * @return string The error message in string format.<br />
   */
  public function getErrorMessage($code = -1) {
    if ($code == -1)
      $code = $_FILES[$this->filename]['error'];

    switch ($code) {
      case 0 :
        return "No error";
      case 1 :
        return "The file is bigger than this PHP installation allows";
      case 2 :
        return "The file is bigger than this form allows";
      case 3 :
        return "Only part of the file was uploaded";
      case 4 :
        return "No file was uploaded";
    }
  }

  /**
   * Returns the size of the file.<br />
   *
   * @return integer The size of the file.
   */
  public function getSize() {
    return $_FILES[$this->filename]['size'];
  }

  /**
   * Define if there is error.<br />
   *
   * @return boolean TRUE if there is an error.
   */
  public function hasError() {
    if ($_FILES[$this->filename]['error'] == 0)
      return FALSE;
    else
      return TRUE;
  }

  /**
   * Define if the uploaded file exists.<br />
   *
   * @return boolean TRUE if the uploaded file exists.
   */
  public function exists() {
    return isset($_FILES[$this->filename]);
  }

  /**
   * Moves the uploaded file to a new location.<br />
   * This public function checks to ensure that the file designated by filename is a
   * valid uploaded file (meaning that it was uploaded via PHP's HTTP POST
   * upload mechanism). If the file is valid, it will be moved to the filename
   * given by destination. This sort of check is especially important if there
   * is any chance that anything done with uploaded files could reveal their
   * contents to the user, or even to other users on the same system.<br />
   *
   * @param $destination The destination of the moved file.
   * @param $filename If given, rename the uploaded file.
   *
   * @return boolean Returns FALSE if the file does not exist or if an error
   * occured.<br /><br />
   *
   * @see <a href="http://be.php.net/manual/en/public function.move-uploaded-file.php">move_uploaded_file()</a>
   */
  public function Move($destination, $filename = "") {
    if (is_string($destination) && is_string($filename))
      if (!empty($filename))
        return move_uploaded_file($_FILES[$this->filename]['tmp_name'],
                                  $destination . $filename);
      else {
        return move_uploaded_file($_FILES[$this->filename]['tmp_name'],
                                  $destination . $_FILES[$this->filename]['name']);
      }
  }

}

?>
